<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<!--        <el-col :span="12" class="mb20">-->
				<!--          <el-form-item label="体检名称" prop="name">-->
				<!--            <el-input v-model="form.name" placeholder="请输入体检名称"/>-->
				<!--          </el-form-item>-->
				<!--        </el-col>-->
				<el-col :span="24" class="mb20">
					<el-form-item label="体检编号" prop="examNo">
						<el-input v-model="form.examNo" :disabled="true" placeholder="请输入体检编号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="开始时间" prop="startTime">
						<el-date-picker type="date" placeholder="请选择开始时间" v-model="form.startTime" :value-format="dateTimeStr"> </el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker type="date" placeholder="请选择结束时间" v-model="form.endTime" :value-format="dateTimeStr"> </el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="开始序号" prop="startNo">
						<el-input v-model="form.startNo" placeholder="请输入开始序号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="结束序号" prop="endNo">
						<el-input v-model="form.endNo" placeholder="请输入结束序号" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="标签张数" prop="sheetsNumber">
						<el-input-number :min="1" :max="1000" v-model="form.sheetsNumber" placeholder="请输入标签张数"></el-input-number>
					</el-form-item>
				</el-col>
				<!--				<el-col :span="12" class="mb20">-->
				<!--					<el-form-item label="是否结束" prop="isEnd">-->
				<!--						<el-radio-group v-model="form.isEnd">-->
				<!--							<el-radio :label="item.value" v-for="(item, index) in bloodx_exam" border :key="index">{{ item.label }} </el-radio>-->
				<!--						</el-radio-group>-->
				<!--					</el-form-item>-->
				<!--				</el-col>-->
				<el-col :span="12" class="mb20">
					<el-form-item label="备注" prop="remark">
						<el-input v-model="form.remark" placeholder="请输入备注" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BloodxExamDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj, getBloodExamNumber } from '/@/api/blood/bloodxExam';
import { rule } from '/@/utils/validate';

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典
const { bloodx_exam } = useDict('bloodx_exam');

// 提交表单数据
const form = reactive({
	id: '',
	name: '',
	examNo: '',
	startTime: '',
	endTime: '',
	isEnd: '',
	sheetsNumber: null,
	startNo: '',
	endNo: '',
	remark: '',
});

// 定义校验规则
const dataRules = ref({
	sheetsNumber: [
		{ validator: rule.number, trigger: 'blur' },
		{ required: true, message: '请输入标签张数', trigger: 'blur' },
	],
	// name: [{required: true, message: '请输入体检名称', trigger: 'blur'}],
	startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
	endTime: [{ required: true, message: '请选择结束时间', trigger: 'blur' }],
	startNo: [
		{ validator: rule.number, trigger: 'blur' },
		{ required: true, message: '请输入开始序号', trigger: 'blur' },
	],
	endNo: [
		{ validator: rule.number, trigger: 'blur' },
		{ required: true, message: '请输入结束序号', trigger: 'blur' },
	],
	examNo: [{ required: true, message: '请输入体检编号', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';
	const nowDate = new Date();
	form.examNo = nowDate.getFullYear() + '_00' + (nowDate.getMonth() + 1);

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取bloodxExam信息
	if (id) {
		form.id = id;
		getbloodxExamData(id);
	} else {
		getBloodExamNumber().then((res) => {
			form.examNo = res.data;
		});
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		let res;
		form.id ? (res = await putObj(form)) : (res = await addObj(form));
		const { code, data, msg } = res;
		if (code === 1001) {
			form.examNo = data;
			useMessage().error(msg);
		} else {
			useMessage().success(form.id ? '修改成功' : '添加成功');
			visible.value = false;
			emit('refresh');
		}
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbloodxExamData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
